<template>
	<view>
		<!-- 收货地址组件 -->
		<view class="address-choose-box" v-if="JSON.stringify(address) === '[]' " >
			<button type="primary" size="mini" class="address-button" @click="chooseAddress">请求收货地址+</button>
		</view>
		<!-- 渲染确切收货信息的盒子 -->
		<view class="address-info-box" v-else @click="chooseAddress">
			<view class="row1">
				<view class="row1-left">
					<view class="username" >
						收货人： {{address.userName}}
					</view>
				</view>
				<view class="row1-right">
					<view class="phone">
						电话：  {{address.telNumber}}
					</view>
					<uni-icons type="right" size="16"></uni-icons>
				</view>
			</view>
			<view class="row2">
				<view class="dizhi">
					收货地址：  {{addstr}}
				</view>
			</view>
		</view>
		<!-- 底部的边框线 -->
		<view class="border-bottom-c"></view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex'
	export default {
		name:"my-address",
		data() {
			return {
				//收货地址的对象
				// address:{}
			};
		},
		methods:{
			//选择收货地址(因为域名服务器证书过期，所以没用)
			async chooseAddress(){
				//调用小程序的chooseAddress方法，就可以选择收货地址的功能
				//返回值是一个数组，[错误对象，成功的地址对象]
				const [err,succ] = await uni.chooseAddress().catch(err => err)
				//如果用户成功的选择了收货地址
				if(err === null && succ.errMsg === 'chooseAddress:ok'){
					console.log(succ)
					this.addToaddress(succ)
				}
				
				if(err && err.errMsg === 'chooseAddress:fail auth deeny'){
					//通过这种方法让用户重新授权
					this.reAuth()
				}
			},
			//让用户重新授权
			async reAuth(){
			   const [err2,confrimResult] = await uni.showModal({
					content:'检测到没有开启地址权限，是否需要打开？',
					confirmText:'确认',
					cancelText:'取消'
				})
				if(err2)return
				
				if(confrimResult.cancel) return uni.$showMsg('你取消了地址授权')
				if(confrimResult.confirm) return console.log('需要进一步授权')
			},
			// getaddress(){
			// 	uni.redirectTo({
			// 		url:'/subpkg/address/address'
			// 	})
			// 	// this.$emit('address-change',{})
			// },
			...mapMutations('m_address', ['addToaddress'])
		},
		computed: {
			...mapGetters('m_address',['addstr']),
			...mapState('m_address',['address'])
		}
	}
</script>

<style lang="scss">
	.address-choose-box{
		height: 90px;
		display: flex;
		justify-content: center;
		align-items: center;
		.address-button{
			
		}
	}
	.address-info-box{
		font-size: 14px;
		height: 90px;
		display: flex;
		flex-direction: column;
		justify-content: space-around;
		padding: 0 5px;
		.row1{
			display: flex;
			justify-content: space-between;
			.row1-left{
				.username{}
			}
			.row1-right{
				display: flex;
				justify-content: space-between;
				.phone{
					
				}
			}
		}
		.row2{
			display: flex;
			justify-content: left;
			align-items: center;
			
			.dizhi{
				white-space:nowrap;
				text-overflow: ellipsis;
				overflow: hidden;
			}
		}
	}
.border-bottom-c{
	height: 10px;
	width: 100%;
	background:linear-gradient(45deg,blue,red,yellow) ;
}
</style>
